
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/06/2013 13:06:11
-- Generated from EDMX file: D:\p\u\university-schedule\DataModel\Models\UniversityModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [UniversityPortal];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Admins_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Admins] DROP CONSTRAINT [FK_Admins_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_Lessons_Auditories]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Lessons] DROP CONSTRAINT [FK_Lessons_Auditories];
GO
IF OBJECT_ID(N'[dbo].[FK_Chair_Departaments]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Chairs] DROP CONSTRAINT [FK_Chair_Departaments];
GO
IF OBJECT_ID(N'[dbo].[FK_Lecturers_Cathedra]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Professors] DROP CONSTRAINT [FK_Lecturers_Cathedra];
GO
IF OBJECT_ID(N'[dbo].[FK_Professors_Degrees]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Professors] DROP CONSTRAINT [FK_Professors_Degrees];
GO
IF OBJECT_ID(N'[dbo].[FK_Specialities_Faculties]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Specialities] DROP CONSTRAINT [FK_Specialities_Faculties];
GO
IF OBJECT_ID(N'[dbo].[FK_Groups_Specialities]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Groups] DROP CONSTRAINT [FK_Groups_Specialities];
GO
IF OBJECT_ID(N'[dbo].[FK_Journals_Groups]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Journals] DROP CONSTRAINT [FK_Journals_Groups];
GO
IF OBJECT_ID(N'[dbo].[FK_Students_Groups]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Students] DROP CONSTRAINT [FK_Students_Groups];
GO
IF OBJECT_ID(N'[dbo].[FK_JournalCell_Journals]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[JournalCells] DROP CONSTRAINT [FK_JournalCell_Journals];
GO
IF OBJECT_ID(N'[dbo].[FK_JournalCell_Lessons]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[JournalCells] DROP CONSTRAINT [FK_JournalCell_Lessons];
GO
IF OBJECT_ID(N'[dbo].[FK_JournalCell_Students]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[JournalCells] DROP CONSTRAINT [FK_JournalCell_Students];
GO
IF OBJECT_ID(N'[dbo].[FK_Lessons_LessonTypes]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Lessons] DROP CONSTRAINT [FK_Lessons_LessonTypes];
GO
IF OBJECT_ID(N'[dbo].[FK_Lessons_Professors]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Lessons] DROP CONSTRAINT [FK_Lessons_Professors];
GO
IF OBJECT_ID(N'[dbo].[FK_Lessons_Subject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Lessons] DROP CONSTRAINT [FK_Lessons_Subject];
GO
IF OBJECT_ID(N'[dbo].[FK_Professors_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Professors] DROP CONSTRAINT [FK_Professors_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_Users_Profiles]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Users] DROP CONSTRAINT [FK_Users_Profiles];
GO
IF OBJECT_ID(N'[dbo].[FK_Students_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Students] DROP CONSTRAINT [FK_Students_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_LessonsToGroups_Groups]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[LessonsToGroups] DROP CONSTRAINT [FK_LessonsToGroups_Groups];
GO
IF OBJECT_ID(N'[dbo].[FK_LessonsToGroups_Lessons]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[LessonsToGroups] DROP CONSTRAINT [FK_LessonsToGroups_Lessons];
GO
IF OBJECT_ID(N'[dbo].[FK_RolesToUsers_Roles]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RolesToUsers] DROP CONSTRAINT [FK_RolesToUsers_Roles];
GO
IF OBJECT_ID(N'[dbo].[FK_RolesToUsers_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RolesToUsers] DROP CONSTRAINT [FK_RolesToUsers_Users];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Admins]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Admins];
GO
IF OBJECT_ID(N'[dbo].[Auditories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Auditories];
GO
IF OBJECT_ID(N'[dbo].[Chairs]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Chairs];
GO
IF OBJECT_ID(N'[dbo].[Degrees]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Degrees];
GO
IF OBJECT_ID(N'[dbo].[Departments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Departments];
GO
IF OBJECT_ID(N'[dbo].[Groups]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Groups];
GO
IF OBJECT_ID(N'[dbo].[JournalCells]', 'U') IS NOT NULL
    DROP TABLE [dbo].[JournalCells];
GO
IF OBJECT_ID(N'[dbo].[Journals]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Journals];
GO
IF OBJECT_ID(N'[dbo].[Lessons]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Lessons];
GO
IF OBJECT_ID(N'[dbo].[LessonTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LessonTypes];
GO
IF OBJECT_ID(N'[dbo].[Professors]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Professors];
GO
IF OBJECT_ID(N'[dbo].[Profiles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Profiles];
GO
IF OBJECT_ID(N'[dbo].[Roles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Roles];
GO
IF OBJECT_ID(N'[dbo].[Specialities]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Specialities];
GO
IF OBJECT_ID(N'[dbo].[Students]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Students];
GO
IF OBJECT_ID(N'[dbo].[Subjects]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Subjects];
GO
IF OBJECT_ID(N'[dbo].[sysdiagrams]', 'U') IS NOT NULL
    DROP TABLE [dbo].[sysdiagrams];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[LessonsToGroups]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LessonsToGroups];
GO
IF OBJECT_ID(N'[dbo].[RolesToUsers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RolesToUsers];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Admins'
CREATE TABLE [dbo].[Admins] (
    [ID] uniqueidentifier  NOT NULL,
    [userID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Auditories'
CREATE TABLE [dbo].[Auditories] (
    [ID] uniqueidentifier  NOT NULL,
    [number] int  NOT NULL,
    [housing] int  NOT NULL,
    [partical] nvarchar(2)  NULL
);
GO

-- Creating table 'Chairs'
CREATE TABLE [dbo].[Chairs] (
    [ID] uniqueidentifier  NOT NULL,
    [shortName] nvarchar(20)  NOT NULL,
    [fullName] nvarchar(100)  NOT NULL,
    [description] nvarchar(1000)  NULL,
    [departmentID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Degrees'
CREATE TABLE [dbo].[Degrees] (
    [ID] uniqueidentifier  NOT NULL,
    [name] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'Departments'
CREATE TABLE [dbo].[Departments] (
    [ID] uniqueidentifier  NOT NULL,
    [shortName] nvarchar(50)  NOT NULL,
    [fullName] nvarchar(200)  NOT NULL,
    [desription] nvarchar(1000)  NULL
);
GO

-- Creating table 'Groups'
CREATE TABLE [dbo].[Groups] (
    [ID] uniqueidentifier  NOT NULL,
    [number] bigint  NOT NULL,
    [specialityID] uniqueidentifier  NOT NULL,
    [entranceDate] datetime  NOT NULL,
    [amountYears] int  NOT NULL
);
GO

-- Creating table 'JournalCells'
CREATE TABLE [dbo].[JournalCells] (
    [ID] uniqueidentifier  NOT NULL,
    [studentID] uniqueidentifier  NOT NULL,
    [date] datetime  NOT NULL,
    [lessonID] uniqueidentifier  NOT NULL,
    [mark] int  NULL,
    [presence] int  NULL,
    [journalID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Journals'
CREATE TABLE [dbo].[Journals] (
    [ID] uniqueidentifier  NOT NULL,
    [groupID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Lessons'
CREATE TABLE [dbo].[Lessons] (
    [ID] uniqueidentifier  NOT NULL,
    [subjectID] uniqueidentifier  NOT NULL,
    [professorID] uniqueidentifier  NOT NULL,
    [auditoryID] uniqueidentifier  NOT NULL,
    [lessonTypeID] uniqueidentifier  NOT NULL,
    [beginTime] time  NOT NULL,
    [endTime] time  NOT NULL,
    [weekDay] smallint  NOT NULL,
    [periodicity] int  NOT NULL,
    [periodicityMask] int  NOT NULL,
    [beginDate] datetime  NOT NULL,
    [endDate] datetime  NOT NULL,
    [semester] int  NOT NULL
);
GO

-- Creating table 'LessonTypes'
CREATE TABLE [dbo].[LessonTypes] (
    [ID] uniqueidentifier  NOT NULL,
    [name] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'Professors'
CREATE TABLE [dbo].[Professors] (
    [ID] uniqueidentifier  NOT NULL,
    [userID] uniqueidentifier  NOT NULL,
    [degreeID] uniqueidentifier  NOT NULL,
    [chairID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Profiles'
CREATE TABLE [dbo].[Profiles] (
    [ID] uniqueidentifier  NOT NULL,
    [description] nvarchar(1000)  NULL
);
GO

-- Creating table 'Roles'
CREATE TABLE [dbo].[Roles] (
    [ID] uniqueidentifier  NOT NULL,
    [name] nvarchar(100)  NOT NULL
);
GO

-- Creating table 'Specialities'
CREATE TABLE [dbo].[Specialities] (
    [ID] uniqueidentifier  NOT NULL,
    [shortName] nvarchar(50)  NOT NULL,
    [fullName] nvarchar(200)  NOT NULL,
    [depatmentID] uniqueidentifier  NOT NULL,
    [description] nvarchar(1000)  NULL
);
GO

-- Creating table 'Students'
CREATE TABLE [dbo].[Students] (
    [ID] uniqueidentifier  NOT NULL,
    [userID] uniqueidentifier  NOT NULL,
    [groupID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Subjects'
CREATE TABLE [dbo].[Subjects] (
    [ID] uniqueidentifier  NOT NULL,
    [shortName] nvarchar(50)  NOT NULL,
    [fullName] nvarchar(200)  NOT NULL,
    [desription] nvarchar(1000)  NULL
);
GO

-- Creating table 'sysdiagrams'
CREATE TABLE [dbo].[sysdiagrams] (
    [name] nvarchar(128)  NOT NULL,
    [principal_id] int  NOT NULL,
    [diagram_id] int IDENTITY(1,1) NOT NULL,
    [version] int  NULL,
    [definition] varbinary(max)  NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [ID] uniqueidentifier  NOT NULL,
    [familyName] nvarchar(50)  NOT NULL,
    [firstName] nvarchar(50)  NOT NULL,
    [lastName] nvarchar(50)  NOT NULL,
    [email] nvarchar(500)  NOT NULL,
    [login] nvarchar(50)  NOT NULL,
    [password] nvarchar(1000)  NOT NULL,
    [profileID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'LessonsToGroups'
CREATE TABLE [dbo].[LessonsToGroups] (
    [Groups_ID] uniqueidentifier  NOT NULL,
    [Lessons_ID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'RolesToUsers'
CREATE TABLE [dbo].[RolesToUsers] (
    [Roles_ID] uniqueidentifier  NOT NULL,
    [Users_ID] uniqueidentifier  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID] in table 'Admins'
ALTER TABLE [dbo].[Admins]
ADD CONSTRAINT [PK_Admins]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Auditories'
ALTER TABLE [dbo].[Auditories]
ADD CONSTRAINT [PK_Auditories]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Chairs'
ALTER TABLE [dbo].[Chairs]
ADD CONSTRAINT [PK_Chairs]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Degrees'
ALTER TABLE [dbo].[Degrees]
ADD CONSTRAINT [PK_Degrees]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Departments'
ALTER TABLE [dbo].[Departments]
ADD CONSTRAINT [PK_Departments]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [PK_Groups]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'JournalCells'
ALTER TABLE [dbo].[JournalCells]
ADD CONSTRAINT [PK_JournalCells]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Journals'
ALTER TABLE [dbo].[Journals]
ADD CONSTRAINT [PK_Journals]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Lessons'
ALTER TABLE [dbo].[Lessons]
ADD CONSTRAINT [PK_Lessons]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'LessonTypes'
ALTER TABLE [dbo].[LessonTypes]
ADD CONSTRAINT [PK_LessonTypes]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Professors'
ALTER TABLE [dbo].[Professors]
ADD CONSTRAINT [PK_Professors]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Profiles'
ALTER TABLE [dbo].[Profiles]
ADD CONSTRAINT [PK_Profiles]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Roles'
ALTER TABLE [dbo].[Roles]
ADD CONSTRAINT [PK_Roles]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Specialities'
ALTER TABLE [dbo].[Specialities]
ADD CONSTRAINT [PK_Specialities]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Students'
ALTER TABLE [dbo].[Students]
ADD CONSTRAINT [PK_Students]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Subjects'
ALTER TABLE [dbo].[Subjects]
ADD CONSTRAINT [PK_Subjects]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [diagram_id] in table 'sysdiagrams'
ALTER TABLE [dbo].[sysdiagrams]
ADD CONSTRAINT [PK_sysdiagrams]
    PRIMARY KEY CLUSTERED ([diagram_id] ASC);
GO

-- Creating primary key on [ID] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [Groups_ID], [Lessons_ID] in table 'LessonsToGroups'
ALTER TABLE [dbo].[LessonsToGroups]
ADD CONSTRAINT [PK_LessonsToGroups]
    PRIMARY KEY NONCLUSTERED ([Groups_ID], [Lessons_ID] ASC);
GO

-- Creating primary key on [Roles_ID], [Users_ID] in table 'RolesToUsers'
ALTER TABLE [dbo].[RolesToUsers]
ADD CONSTRAINT [PK_RolesToUsers]
    PRIMARY KEY NONCLUSTERED ([Roles_ID], [Users_ID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [userID] in table 'Admins'
ALTER TABLE [dbo].[Admins]
ADD CONSTRAINT [FK_Admins_Users]
    FOREIGN KEY ([userID])
    REFERENCES [dbo].[Users]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Admins_Users'
CREATE INDEX [IX_FK_Admins_Users]
ON [dbo].[Admins]
    ([userID]);
GO

-- Creating foreign key on [auditoryID] in table 'Lessons'
ALTER TABLE [dbo].[Lessons]
ADD CONSTRAINT [FK_Lessons_Auditories]
    FOREIGN KEY ([auditoryID])
    REFERENCES [dbo].[Auditories]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Lessons_Auditories'
CREATE INDEX [IX_FK_Lessons_Auditories]
ON [dbo].[Lessons]
    ([auditoryID]);
GO

-- Creating foreign key on [departmentID] in table 'Chairs'
ALTER TABLE [dbo].[Chairs]
ADD CONSTRAINT [FK_Chair_Departaments]
    FOREIGN KEY ([departmentID])
    REFERENCES [dbo].[Departments]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Chair_Departaments'
CREATE INDEX [IX_FK_Chair_Departaments]
ON [dbo].[Chairs]
    ([departmentID]);
GO

-- Creating foreign key on [chairID] in table 'Professors'
ALTER TABLE [dbo].[Professors]
ADD CONSTRAINT [FK_Lecturers_Cathedra]
    FOREIGN KEY ([chairID])
    REFERENCES [dbo].[Chairs]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Lecturers_Cathedra'
CREATE INDEX [IX_FK_Lecturers_Cathedra]
ON [dbo].[Professors]
    ([chairID]);
GO

-- Creating foreign key on [degreeID] in table 'Professors'
ALTER TABLE [dbo].[Professors]
ADD CONSTRAINT [FK_Professors_Degrees]
    FOREIGN KEY ([degreeID])
    REFERENCES [dbo].[Degrees]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Professors_Degrees'
CREATE INDEX [IX_FK_Professors_Degrees]
ON [dbo].[Professors]
    ([degreeID]);
GO

-- Creating foreign key on [depatmentID] in table 'Specialities'
ALTER TABLE [dbo].[Specialities]
ADD CONSTRAINT [FK_Specialities_Faculties]
    FOREIGN KEY ([depatmentID])
    REFERENCES [dbo].[Departments]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Specialities_Faculties'
CREATE INDEX [IX_FK_Specialities_Faculties]
ON [dbo].[Specialities]
    ([depatmentID]);
GO

-- Creating foreign key on [specialityID] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [FK_Groups_Specialities]
    FOREIGN KEY ([specialityID])
    REFERENCES [dbo].[Specialities]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Groups_Specialities'
CREATE INDEX [IX_FK_Groups_Specialities]
ON [dbo].[Groups]
    ([specialityID]);
GO

-- Creating foreign key on [groupID] in table 'Journals'
ALTER TABLE [dbo].[Journals]
ADD CONSTRAINT [FK_Journals_Groups]
    FOREIGN KEY ([groupID])
    REFERENCES [dbo].[Groups]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Journals_Groups'
CREATE INDEX [IX_FK_Journals_Groups]
ON [dbo].[Journals]
    ([groupID]);
GO

-- Creating foreign key on [groupID] in table 'Students'
ALTER TABLE [dbo].[Students]
ADD CONSTRAINT [FK_Students_Groups]
    FOREIGN KEY ([groupID])
    REFERENCES [dbo].[Groups]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Students_Groups'
CREATE INDEX [IX_FK_Students_Groups]
ON [dbo].[Students]
    ([groupID]);
GO

-- Creating foreign key on [journalID] in table 'JournalCells'
ALTER TABLE [dbo].[JournalCells]
ADD CONSTRAINT [FK_JournalCell_Journals]
    FOREIGN KEY ([journalID])
    REFERENCES [dbo].[Journals]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_JournalCell_Journals'
CREATE INDEX [IX_FK_JournalCell_Journals]
ON [dbo].[JournalCells]
    ([journalID]);
GO

-- Creating foreign key on [lessonID] in table 'JournalCells'
ALTER TABLE [dbo].[JournalCells]
ADD CONSTRAINT [FK_JournalCell_Lessons]
    FOREIGN KEY ([lessonID])
    REFERENCES [dbo].[Lessons]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_JournalCell_Lessons'
CREATE INDEX [IX_FK_JournalCell_Lessons]
ON [dbo].[JournalCells]
    ([lessonID]);
GO

-- Creating foreign key on [studentID] in table 'JournalCells'
ALTER TABLE [dbo].[JournalCells]
ADD CONSTRAINT [FK_JournalCell_Students]
    FOREIGN KEY ([studentID])
    REFERENCES [dbo].[Students]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_JournalCell_Students'
CREATE INDEX [IX_FK_JournalCell_Students]
ON [dbo].[JournalCells]
    ([studentID]);
GO

-- Creating foreign key on [lessonTypeID] in table 'Lessons'
ALTER TABLE [dbo].[Lessons]
ADD CONSTRAINT [FK_Lessons_LessonTypes]
    FOREIGN KEY ([lessonTypeID])
    REFERENCES [dbo].[LessonTypes]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Lessons_LessonTypes'
CREATE INDEX [IX_FK_Lessons_LessonTypes]
ON [dbo].[Lessons]
    ([lessonTypeID]);
GO

-- Creating foreign key on [professorID] in table 'Lessons'
ALTER TABLE [dbo].[Lessons]
ADD CONSTRAINT [FK_Lessons_Professors]
    FOREIGN KEY ([professorID])
    REFERENCES [dbo].[Professors]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Lessons_Professors'
CREATE INDEX [IX_FK_Lessons_Professors]
ON [dbo].[Lessons]
    ([professorID]);
GO

-- Creating foreign key on [subjectID] in table 'Lessons'
ALTER TABLE [dbo].[Lessons]
ADD CONSTRAINT [FK_Lessons_Subject]
    FOREIGN KEY ([subjectID])
    REFERENCES [dbo].[Subjects]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Lessons_Subject'
CREATE INDEX [IX_FK_Lessons_Subject]
ON [dbo].[Lessons]
    ([subjectID]);
GO

-- Creating foreign key on [userID] in table 'Professors'
ALTER TABLE [dbo].[Professors]
ADD CONSTRAINT [FK_Professors_Users]
    FOREIGN KEY ([userID])
    REFERENCES [dbo].[Users]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Professors_Users'
CREATE INDEX [IX_FK_Professors_Users]
ON [dbo].[Professors]
    ([userID]);
GO

-- Creating foreign key on [profileID] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [FK_Users_Profiles]
    FOREIGN KEY ([profileID])
    REFERENCES [dbo].[Profiles]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Users_Profiles'
CREATE INDEX [IX_FK_Users_Profiles]
ON [dbo].[Users]
    ([profileID]);
GO

-- Creating foreign key on [userID] in table 'Students'
ALTER TABLE [dbo].[Students]
ADD CONSTRAINT [FK_Students_Users]
    FOREIGN KEY ([userID])
    REFERENCES [dbo].[Users]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Students_Users'
CREATE INDEX [IX_FK_Students_Users]
ON [dbo].[Students]
    ([userID]);
GO

-- Creating foreign key on [Groups_ID] in table 'LessonsToGroups'
ALTER TABLE [dbo].[LessonsToGroups]
ADD CONSTRAINT [FK_LessonsToGroups_Groups]
    FOREIGN KEY ([Groups_ID])
    REFERENCES [dbo].[Groups]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Lessons_ID] in table 'LessonsToGroups'
ALTER TABLE [dbo].[LessonsToGroups]
ADD CONSTRAINT [FK_LessonsToGroups_Lessons]
    FOREIGN KEY ([Lessons_ID])
    REFERENCES [dbo].[Lessons]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LessonsToGroups_Lessons'
CREATE INDEX [IX_FK_LessonsToGroups_Lessons]
ON [dbo].[LessonsToGroups]
    ([Lessons_ID]);
GO

-- Creating foreign key on [Roles_ID] in table 'RolesToUsers'
ALTER TABLE [dbo].[RolesToUsers]
ADD CONSTRAINT [FK_RolesToUsers_Roles]
    FOREIGN KEY ([Roles_ID])
    REFERENCES [dbo].[Roles]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Users_ID] in table 'RolesToUsers'
ALTER TABLE [dbo].[RolesToUsers]
ADD CONSTRAINT [FK_RolesToUsers_Users]
    FOREIGN KEY ([Users_ID])
    REFERENCES [dbo].[Users]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RolesToUsers_Users'
CREATE INDEX [IX_FK_RolesToUsers_Users]
ON [dbo].[RolesToUsers]
    ([Users_ID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------